#![no_std]
#![no_main]

use esp_hal::timer::systimer::SystemTimer;
use esp_hal::{clock::CpuClock, delay::Delay, main};

#[cfg(feature = "defmt")]
use defmt;
#[cfg(feature = "defmt")]
use defmt_rtt as _;

#[cfg(any(feature = "log", feature = "jtag", feature = "uart"))]
use esp_println::println;

#[cfg(any(feature = "log", feature = "jtag", feature = "uart"))]
use esp_backtrace as _;
 

#[cfg(feature = "defmt")]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
    defmt::info!("Panic: {}", info);
    loop {}
}


#[cfg(feature = "rtt")]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
    log::error!("Panic: {}", info);
    loop {}
}


#[main]
fn main() -> ! {
    #[cfg(feature = "log")]
    {
        esp_println::logger::init_logger(log::LevelFilter::Info);
    }

   #[cfg(feature = "rtt")]
    {
       rtt_log::init();
    }


    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);
    let _timer0 = SystemTimer::new(peripherals.SYSTIMER);

    #[cfg(any(feature = "log", feature = "jtag", feature = "uart"))]
    {
        println!("hello world!");
    }

    #[cfg(feature = "log")]
    {
        log::info!("timer0--->{:?}", _timer0.alarm0);
        log::error!("this is error message");
        log::warn!("this is warn message");
        log::info!("this is info message");
        log::debug!("this is debug message");
        log::trace!("this is trace message");
    }

     #[cfg(feature = "rtt")]
    { 
        log::error!("rtt log is error message");
        log::warn!("rtt log is warn message");
        log::info!("rtt log is info message");
        log::debug!("rtt log is debug message");
        log::trace!("rtt log is trace message");
    }


    #[cfg(feature = "defmt")]
    {
        defmt::error!("this is error message");
        defmt::warn!("this is warn message");
        defmt::info!("this is info message");
        defmt::debug!("this is debug message");
        defmt::trace!("this is trace message");
    }

    let delay = Delay::new();
    loop {
        #[cfg(feature = "log")]
        {
            log::info!("this is info message");
        }
        #[cfg(any(feature = "log", feature = "jtag", feature = "uart"))]
        {
            println!("hello world!");
        }

        #[cfg(feature = "defmt")]
        {
            defmt::info!("defmt: this is info message");
        }

        #[cfg(feature = "rtt")]
        {
            log::info!("rtt log info message");
        }
        delay.delay_millis(2000);
    }
}
